<template>
  <div>
    <div class="add-permission">
      <el-input v-model="row.name" size="small" placeholder="请输入角色英文名">
        <template slot="prepend">ROLE_</template>
      </el-input>
      <el-input v-model="row.nameZh" size="small" placeholder="请输入角色中文名" class="add-permission-input" @keydown.enter.native="createRole" />
      <el-button type="primary" size="small" @click="createRole">添加角色</el-button>
    </div>
    <div class="main-permission">
      <el-collapse v-model="activeNames" accordion @change="change">
        <el-collapse-item v-for="(roleItem, roleIndex) in roles" :key="roleIndex" :title="roleItem.nameZh" :name="roleItem.id">
          <div>
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span>可访问资源</span>
                <el-button style="float: right; padding: 3px 0; color: #F56C6C" type="text" icon="el-icon-delete" @click="deleteRole(roleItem)" />
              </div>
              <div>
                <el-tree ref="tree" :key="roleIndex" node-key="id" :default-checked-keys="selectedMenus" show-checkbox :data="menus" :props="defaultProps" />
                <div style="display: flex;justify-content: flex-end">
                  <el-button size="small" @click="updateMenuRoleCancel">取消修改</el-button>
                  <el-button type="primary" size="small" @click="updateMenuRoleConfirm(roleItem.id, roleIndex)">确认修改</el-button>
                </div>
              </div>
            </el-card>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script>
import { GetAll as GetAllRole, Create as CreateRole, Delete as DeleteRole } from '@/api/Sys/SysBasic/PermissionManage/Role'
import { GetAll as GetAllMenu } from '@/api/Sys/SysBasic/PermissionManage/Menu'
import { getMIdsByRId, updateMenuRole } from '@/api/Sys/SysBasic/PermissionManage/MenuRole'

export default {
  name: 'PermissionManage',
  data() {
    return {
      row: {
        name: '',
        nameZh: ''
      },
      roles: [],
      menus: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      selectedMenus: [],
      activeNames: -1
    }
  },
  mounted() {
    this.GetAllRole()
  },
  methods: {
    GetAllRole() {
      GetAllRole().then((res) => {
        if (res) {
          this.roles = res
        }
      })
    },
    change(rid) {
      if (rid) {
        this.GetAllMenu()
        this.getMIdsByRId(rid)
      }
    },
    GetAllMenu() {
      GetAllMenu().then((res) => {
        if (res) {
          this.menus = res
        }
      })
    },
    getMIdsByRId(rid) {
      this.selectedMenus = []
      getMIdsByRId(rid).then((res) => {
        if (res) {
          this.selectedMenus = res
        }
      })
    },
    updateMenuRoleConfirm(rid, index) {
      const tree = this.$refs.tree[index]
      const selectedKeys = tree.getCheckedKeys(true)
      var p = 'rid=' + rid + '&'
      selectedKeys.forEach(element => {
        p = p + 'mids=' + element + '&'
      })
      updateMenuRole(p).then((res) => {
        if (res) {
          this.getMIdsByRId(rid)
          this.activeNames = -1
        }
      })
    },
    updateMenuRoleCancel() {
      this.activeNames = -1
    },
    createRole() {
      if (this.row.name && this.row.nameZh) {
        CreateRole(this.row).then((res) => {
          if (res) {
            this.GetAllRole()
            this.row.name = ''
            this.row.nameZh = ''
          }
        })
      } else {
        this.$message.warning('角色英文名和角色中文名不可以为空!')
      }
    },
    deleteRole(role) {
      this.$confirm('此操作将永久删除【' + role.nameZh + '】职位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        DeleteRole(role.id).then((res) => {
          if (res) {
            this.GetAllRole()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.add-permission {
  display: flex;
  flex-direction: row;

  .el-input {
    width: 250px;
  }

  .add-permission-input {
    margin: 0 10px;
  }
}
.main-permission {
  margin: 10px;
  width: 600px;
}
</style>
